package com.sgcc.pda.safe.impl;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.sgcc.pda.hzaidl.entity.RequestBean;
import com.sgcc.pda.hzaidl.entity.ResponseBean;
import com.sgcc.pda.hzaidl.entity.SafeCallback;
import com.sgcc.pda.safe.AIDLSafeServiceImpl;
import com.sgcc.pda.safe.SafeAidlCallback;
import com.sgcc.pda.tools.JsonUtils;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.pda.util.PlatConstant;
import com.sgcc.platforminterface.PlatfInterfManager;

/**
 * 描述：安全管控服务交互工具类
 * 作者：LiuChang 2020/09/17 10:06
 * 修改描述：
 * 修改人：
 * 修改版本：
 */
public class SafeUtil {

    private static AIDLSafeServiceImpl getAIDLService() {
        return AIDLSafeServiceImpl.getInstance();
    }

    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     * @param paramJson  参数列表
     */
    public static void request(String methodName, String paramJson, final SafeCallback callback) {
        if (!SafeManager.isEnabled()) return;//查看是否启用安全管控服务验证
        RequestBean requestBean = getSafe(methodName, paramJson);
        if (callback == null) {
            getAIDLService().post(requestBean.toString(), null);
        } else {
            requestBean.setTag(callback.getTag());
            getAIDLService().post(requestBean.toString(), new SafeAidlCallback.Stub() {
                @Override
                public void callback(final String result) {
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Log.e("safe-request", result);
                                ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
                                if (responseBean != null) {
                                    //如果标识不为空则需要比对，返回不进行比对标识,如果标识是空，或者标识不为空等于返回标识则通过
                                    if (TextUtils.isEmpty(callback.getTag()) || (!TextUtils.isEmpty(callback.getTag()) && TextUtils.equals(responseBean.getTag(), callback.getTag()))) {
                                        switch (responseBean.getRetCode()) {
                                            case 1://调用成功
                                                callback.onSuccess(responseBean.getMsgJson());
                                                break;
                                            default:
                                                callback.onFail(PlatConstant.IWebState.return_code_error, "安全管控服务返回错误");
                                                break;
                                        }
                                    } else {
                                        LogPrintUtils.d(String.format("标识不匹配：请求标识，%s  返回标识，%s", callback.getTag(), responseBean.getTag()));
                                        callback.onFail(PlatConstant.IWebState.return_code_error, "安全管控服务返回错误");
                                    }
                                } else {
                                    //异常信息
                                    LogPrintUtils.d(String.format("安全管控服务返回解析错误 %s", PlatConstant.IWebState.encrypterror));
                                    callback.onFail(PlatConstant.IWebState.encrypterror, "安全管控服务返回解析错误");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                LogPrintUtils.d(String.format("安全管控服务返回产生异常 %s", PlatConstant.IWebState.error));
                                callback.onFail(PlatConstant.IWebState.error, "安全管控服务返回产生异常");
                            }
                        }
                    });
                }
            });
        }
    }

    /**
     * 安全管控服务方法组织请求参数
     *
     * @param methodName 方法名
     * @param paramJson  参数
     * @return 返回请求对像
     */
    private static RequestBean getSafe(String methodName, String paramJson) {
        return new RequestBean(PlatConstant.IWebCmd.SAFE, methodName, paramJson, PlatfInterfManager.getInstance().getPackageName(), "");
    }
}
